package com.xquant.marketdata.marketdata.service.impl;


import com.xquant.common.core.domain.Underlying;
import com.xquant.marketdata.marketdata.mapper.InstrumentMapper;
import com.xquant.common.core.constant.Constants;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;


public class InstrumentRedisThread implements Callable<List<Underlying>> {

    private InstrumentMapper instrumentMapper;

    private RedisTemplate redisTemplate;


    private String aType;//查询条件 根据条件来定义该类的属性

    private int start;//分页index
    private int end;//分页index

    /**
     * 重新构造方法
     *
     * @param instrumentMapper
     * @param aType
     * @param start
     * @param end
     */
    public InstrumentRedisThread(InstrumentMapper instrumentMapper, RedisTemplate redisTemplate, String aType, int start, int end) {
        this.instrumentMapper = instrumentMapper;
        this.redisTemplate = redisTemplate;
        this.aType = aType;
        this.start = start;
        this.end = end;
    }

    @Override
    public List<Underlying> call() throws Exception {
        Map map = new HashMap();
        map.put("start", String.valueOf(start));
        map.put("end", String.valueOf(end));
        //通过service查询得到对应结果
        List<Underlying> list = instrumentMapper.selectUnderlyingDatas(map);
        handleSave(list);
        //handleHashSave(list);
        return list;
    }

    //RedisTemplate使用PipeLine  string
    //使用管道存储
    public void handleSave(List<Underlying> underlyingList) {
        List resultList = redisTemplate.executePipelined(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                connection.openPipeline();
                String tempKey = "";
                for (Underlying underlying : underlyingList) {
                    tempKey = underlying.getCode().toUpperCase();
                    connection.set((Constants.SYS_UNDERLYING_MARKET_KEY + tempKey).getBytes(), redisTemplate.getValueSerializer().serialize(underlying));
                }
                return null;
            }
        });

    }

    //RedisTemplate使用PipeLine   hash
    //使用管道存储
    public void handleHashSave(List<Underlying> underlyingList) {
        List resultList = redisTemplate.executePipelined(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                connection.openPipeline();
                String tempKey = "";
                for (Underlying underlying : underlyingList) {
                    tempKey = underlying.getCode().toUpperCase();
                    connection.hSet(Constants.SYS_UNDERLYING_MARKET_KEY.getBytes(), tempKey.getBytes(), redisTemplate.getValueSerializer().serialize(underlying));
                }
                return null;
            }
        });

    }
}

